var Subscriptions;

(function () {
    var instance;
    
    Subscriptions = function () {
        var settings, get, url, getDataProcessor, startIndex, params,
                background;
        
        if (instance) {
            return instance;
        }
        instance = this;
        
        settings = new Settings();
        background = new Background();
        get = new Get();
        
        this.flushCache = function () {
            localStorage.setItem('subscriptions', '');
        };

        this.loadSubscriptions = function (callback, errorCallback) {
            var subscriptions;
            url = 'https://gdata.youtube.com/feeds/api/users/default/' +
                    'subscriptions';
            params = {
                'v': 2,
                'alt': 'json',
                'max-results': 50
            };
            startIndex = 0;
            subscriptions = localStorage.getItem('subscriptions');
            if (subscriptions) {
                subscriptions = JSON.parse(subscriptions);
                if ((new Date()).getTime() - settings.getSubsTimeout() * 1000 <
                        subscriptions.time) {
                    callback(subscriptions.data);
                    return;
                }
            }
            subscriptions = [];
            background.getWithOAuth(url, params,
                    getDataProcessor(subscriptions, callback, errorCallback),
                    errorCallback);
        };
        
        getDataProcessor = function (subscriptions, callback, errCallback) {
            return function (text) {
                var data, i, total, login, j, present;
                data = JSON.parse(text);
                total = data.feed['openSearch$totalResults']['$t'];
                data = data.feed.entry;
                if (data) {
                    for (i = data.length; i--;) {
                        login = data[i]['yt$username']['$t'];
                        present = false;
                        for (j = subscriptions.length; j--;) {
                            if (subscriptions[j] == login) {
                                present = true;
                            }
                        }
                        if (!present) {
                            subscriptions.push(login);
                        }
                    }
                    if (subscriptions.length < total) {
                        // more items to retrieve
                        startIndex += 40; // because YouTube API has bugs :(
                        params = {
                            'v': 2,
                            'alt': 'json',
                            'max-results': 50,
                            'start-index': startIndex
                        };
                        background.getWithOAuth(url, params,
                                getDataProcessor(subscriptions, callback,
                                errCallback), errCallback);
                        return;
                    }
                }
                localStorage.setItem('subscriptions', JSON.stringify({
                    time: (new Date()).getTime(),
                    data: subscriptions
                }));
                callback(subscriptions);
            };
        };
        
        return instance;
    };
}());
